package com.huifenqi.saas.controller;

import com.huifenqi.common.HfqResponse;
import com.huifenqi.model.Meter;
import com.huifenqi.model.User;
import com.huifenqi.saas.client.ExternalClient;
import com.huifenqi.saas.client.MeterClient;
import com.huifenqi.saas.service.UserService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.Date;

/**
 * @author lisheng
 * @description
 * @date 2018/11/1 17:59
 * @version:
 */
@RestController
@RequestMapping("/saas")
@Slf4j
public class SaaSController {

    @Autowired
    private UserService userService;

    @Autowired
    private MeterClient meterClient;

    @Autowired
    private ExternalClient externalClient;


    @PostMapping(value = "/save/user")
    public HfqResponse saveUser(User user) {
        log.info("sass save user service");
        userService.upsert(user);
        return HfqResponse.<User>builder().code(1).msg("保存用户成功").build();
    }

    @GetMapping(value = "/get/user/{id}")
    public HfqResponse getUser(@PathVariable("id") Integer id) {
        log.info("sass get user service");
        User user = userService.get(id);
        return HfqResponse.<User>builder().code(1).msg("查询用户成功").data(user).build();
    }

    @GetMapping(value = "/get/meter/{id}")
    public HfqResponse getMeter(@PathVariable("id") Integer id) {
        log.info("sass get meter");
        return meterClient.getMeter(id);
    }

    @GetMapping(value = "/save/meter")
    public HfqResponse saveMeter() {
        log.info("sass save meter");
        Meter meter = Meter.builder().id(1).ammeterSn("43000000001").userId(1).bindTime(new Date()).build();
        return meterClient.saveMeter(meter);
    }


    @GetMapping(value = "/upload")
    @SneakyThrows
    public String upload() {
        log.info("sass upload");
        File file = new File("E:/test/test.txt");
        return "";
    }

    @GetMapping(value = "/download")
    @SneakyThrows
    public ResponseEntity<byte[]> download() {
        log.info("sass download");
        return meterClient.download();
    }

    @GetMapping(value = "/url/test")
    public HfqResponse urlTest() {
        log.info("sass url test");
        return externalClient.getBiz();
    }

}
